package com.singlemountain.gcommerce.web.account;


import com.singlemountain.gcommerce.domain.address.Addressd;
import com.singlemountain.gcommerce.domain.user.Userd;
import com.singlemountain.gcommerce.web.BaseController;
import com.singlemountain.gcommerce.web.BaseValidators;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AddressAddController extends BaseController<Userd, BaseValidators> {
    public final String URL_MY_ACCOUNT_ADDRESS_ADD = "/myAccountAddressAdd";
    public final String URL_MY_ACCOUNT_ADDRESS_LIST = "/myAccountAddressList";
    public final String URL_MY_ACCOUNT_ADDRESS_DEFAULT = "/myAccountAddressDefault";
    public final String URL_MY_ACCOUNT_ADDRESS_DELETE = "/myAccountAddressDelete";

    public final String VIEW_MY_ACCOUNT_ADDRESS_ADD = "view_my_account_address_add";
    public final String VIEW_MY_ACCOUNT_ADDRESS_LIST = "view_my_account_address_list";

    Userd userd;

    //添加/修改地址
    @RequestMapping(value = URL_MY_ACCOUNT_ADDRESS_ADD, method = RequestMethod.GET)
    public String setUpPage() {
        return VIEW_MY_ACCOUNT_ADDRESS_ADD;
    }

    //保存或修改地址
    @RequestMapping(value = URL_MY_ACCOUNT_ADDRESS_ADD, method = RequestMethod.POST)
    public String handle(Addressd addressd, @RequestParam(value = "id", required = false) Long id) {

        if (id == null || id == 0) {
            userd.getAddressSet().add(addressd);
            userd.merge();
        } else {
            addressd.merge();
        }
        return VIEW_REDIRECT + URL_MY_ACCOUNT_ADDRESS_LIST + "?isEdit=true";
    }

    //返回地址列表
    @RequestMapping(value = URL_MY_ACCOUNT_ADDRESS_LIST, method = RequestMethod.GET)
    public String list(ModelMap modelMap, @RequestParam(value = "isEdit", required = false) Boolean isEdit) {
        modelMap.addAttribute("addressdSet", userd.getAddressSet());
        modelMap.addAttribute("isEdit", isEdit);
        return VIEW_MY_ACCOUNT_ADDRESS_LIST;
    }

    //删除地址
    @RequestMapping(value = URL_MY_ACCOUNT_ADDRESS_DELETE, method = RequestMethod.GET)
    public String editDelete(@RequestParam(value = "id") Long id) {
        Addressd addressd2 = addressdService.findAddressd(id);
        addressd2.remove();
        return VIEW_REDIRECT + URL_MY_ACCOUNT_ADDRESS_LIST + "?isEdit=true";
    }

    //设置默认地址
    @RequestMapping(value = URL_MY_ACCOUNT_ADDRESS_DEFAULT, method = RequestMethod.GET)
    public String editMakeDefault(ModelMap modelMap, @RequestParam(value = "id") Long id) {
        for (Addressd addressd : userd.getAddressSet()) {
            if (addressd.getIsPrimary()) {
                addressd.setIsPrimary(false);
                addressd.merge();
                break;
            }
        }
        Addressd addressd2 = addressdService.findAddressd(id);
        addressd2.setIsPrimary(true);
        addressd2.merge();

        return VIEW_REDIRECT + URL_MY_ACCOUNT_ADDRESS_LIST + "?isEdit=true";
    }


    @ModelAttribute
    public Addressd setModel(HttpSession session, @RequestParam(value = "id", required = false) Long id) {
        userd = getLoginUser(session);
        Addressd addressd;
        if (id == null || id == 0)
            addressd = new Addressd();
        else
            addressd = Addressd.findAddressd(id);
        return addressd;
    }
}
